// pages/ticket_center/ticket_center.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketList: [],
    ishow: true, //显示暂无数据
    searchWord: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCouponList();

  },
  getCouponList: function (searchWord) {
    var that = this;
    //获取优惠券列表
    wx.request({
      url: app.d.hostUrl + '/api/coupon/getCouponList',
      method: 'post',
      data: {
        sessionkey: wx.getStorageSync('sessionkey'),
        page: 1,
        name: searchWord ? searchWord : '',
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.stopPullDownRefresh();
        console.log(res);
        var ticketList = res.data;
        if (ticketList.code == 200) {
          that.setData({
            ticketList: ticketList.data,
            ishow: true
          });
        } else {
          that.setData({
            ishow: false
          });
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon: "none",
          duration: 2000
        });
      },
    });
  },
  getCoupon: function (e) {
    var shop_id = e.currentTarget.dataset.id;
    var canget = e.currentTarget.dataset.canget;
    console.log(canget);
    if (canget != 1) {
      return;
    }
    wx.request({
      url: app.d.hostUrl + '/api/coupon/couponGet',
      method: 'post',
      data: {
        shop_id: shop_id,
        sessionkey: wx.getStorageSync('sessionkey'),
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.stopPullDownRefresh()
        console.log(res);
        var ticketList = res.data;
        if (ticketList.code == 200) {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          });

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          });
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon: "none",
          duration: 2000
        });
      },
    });
  },
  /**
   * 搜索
   */
  onSearch: function (e) {
    console.log(e.detail)
    this.getCouponList(e.detail);
  },
  onCancel: function (e) {
    this.getCouponList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("onPullDownRefresh")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom")

  },

  // 拨打电话
  opentel: function () {
    app.gotel();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var openid = wx.getStorageSync('openid');
    from: menu;
    return {
      title: '走吧商城',
      path: '/pages/onepage/onepage?openid=' + openid,
      success: function (res) {
        // console.log(openid);      
      },
      fail: function (res) {
        // console.log(res);
      }
    }
  },

})